//const AppId = 'wx7638be4b0d460a20';//测试
//const serviceUrl = 'http://weixin.yyjzt.com'
//const serviceUrl = 'http://testb.yyjzt.com'
const serviceUrl = 'http://vincent.ngrok.yyjzt.com'
const devModel=true;//为true的时候使用虚拟数据

function Toast(msg, duration) {
    duration = isNaN(duration) ? 2000 : duration;
    var m = document.createElement('div');
    m.innerHTML = msg;
    m.style.cssText = "width:4.2rem; background:#000; opacity:0.5; color:#fff; line-height:0.33rem;padding-top:0.11rem;padding-bottom:0.11rem; text-align:center; border-radius:5px; position:fixed; bottom:1.5rem; left:1.65rem; z-index:999999;font-size:0.22rem";
    document.body.appendChild(m);
    setTimeout(function () {
        var d = 0.5;
        m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
        m.style.opacity = '0';
        setTimeout(function () {
            document.body.removeChild(m)
        }, d * 1000);
    }, duration);
}

function Load(isShow) {
    if(isShow){
        $('.loadingBack').removeClass('uhide');
    }
    else{
        $('.loadingBack').addClass('uhide');
    }
}

//获取url参数
function GetRequest() {
    var url = window.location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

// function vueRequest(url,data,that,callBack){   
//     data.timeStamp=(new Date().getTime());
//     that.$http.get(serviceUrl + url, {params:data}).then(function(res){
//         // 响应成功回调
//         console.log('响应成功回调：');
//         console.log(res);
//         if(res.status==200){
//             var result=res.body;
//             if(result.success){
//                 callBack(result,that);
//             }else{
//                 alert(result.msg||'接口返回异常数据！');
//             }                  
//         }else{
//             alert('服务响应发生异常！');
//         }     
//     }, function(response){
//         // 响应错误回调
//         console.log('响应错误回调：');
//         console.log(response);
//         alert('服务请求发生异常！');
//     });
// }


function vueRequest(url,data,that,callBack,isInBack){
    data.timestamp=(new Date()).getTime();
    var reqUrl=url;
    if(devModel){
       reqUrl=serviceUrl+reqUrl;
    }
    $.ajax({
        type: "GET",
        timeout: (1000 * 60 * 1),
        url:reqUrl,
        data:data,
        success: function (result) {  
            this.reciveTime = new Date();         
            if(typeof result=='string'){
               result = JSON.parse(result);
            }
            if(result.success){           
                callBack(result,that);
            }else{
                Load(false);
                alert(result.msg||'接口返回异常数据！');
            }                 
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) { 
            Load(false);       
            if (textStatus == 'timeout') {              
                alert('服务器连接超时,请稍后重试！');
            } else if (textStatus == 'error') {
                alert('服务器连接错误，请联系管理员！(' + (errorThrown || '') + '[' + XMLHttpRequest.status + '])');
            } else if (textStatus == 'parsererror') {
                alert('服务器解析错误，请联系管理员！');
            } else if (textStatus == 'abort') {
                console.log('服务器中止请求');
            } else {
                Toast('请求服务器异常', 2000);
            }          
        },
        beforeSend: function () {
            if(!isInBack){
                Load(true);
            }    
            this.sendTime = new Date();    
            console.log('beforeSend:'+ url);
        },
        complete: function () {
            Load(false);
            console.log(url+'  请求完成，耗时:------------------------------------------' + (this.reciveTime.getTime() - this.sendTime.getTime()) / 1000 + '秒！！！');         
        }
    });
}